import axios from "axios"

export default function Index() {
  const [token, setToken] = useState<string>('')
  const [path, setPath] = useState('')
  const [username, setUsername] = useState('zhang')
  const [password, setPassword] = useState('123456')
  const [isLogin, setIsLogin] = useState(false)
  function login() {
    axios('http://localhost:8080' + '/acc/doLogin', {
      params: {
        name: username,
        pwd: password
      }
    }).then(res => {
      const data = res.data
      console.log(data)
      setToken(data.data.token)
    })
  }
  function handler() {
    axios('http://localhost:8080' + path, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    }).then(res => {
      const data = res.data
      setIsLogin(data.data.isLogin)
      console.log('data',data)
    })
  }
  return (
    <div>
      <div>
        <div className="flex flex-col">
          <input type="text" value={username} onChange={e => setUsername(e.target.value)}/>
          <input type="text" value={password} onChange={e => setPassword(e.target.value)}/>
        </div>
        <p>Token: {token}</p>
        <button className="btn" onClick={login}>Log in</button>
      </div>
      <div>
        <input type="text" value={path} onChange={e => setPath(e.target.value)} />
      </div>
      <button className="btn" onClick={handler}>Submit</button>
      <p>{`${isLogin}`}</p>
    </div>
  )
}
